﻿using System;
using System.ServiceModel;
using System.ServiceModel.Channels;
using LinFu.IoC.Configuration;

namespace CoreEx.ServiceModel.Implementation
{    
    /// <summary>
    /// Provides a default <see cref="IBindingProvider"/> implementation that 
    /// resolves the type of <see cref="Binding"/> based on the <see cref="Uri.Scheme"/> or the <see cref="Uri"/>
    /// </summary>
    /// <seealso cref="IBindingProvider"/>
    [Implements(typeof(IBindingProvider))]
    public class DefaultBindingProvider : IBindingProvider
    {
        public Binding CreateBinding(Type serviceType, Uri uri)
        {
            Binding binding;
            switch (uri.Scheme)
            {
                case "net.tcp":
                    binding = new NetTcpBinding(SecurityMode.None);                    
                    break;
                case "http":
                case "https":                    
                    binding = new WSHttpBinding(SecurityMode.None);
                    break;
                case "net.pipe":
                    binding = new NetNamedPipeBinding();
                    break;
                default:
                    throw new ArgumentOutOfRangeException("uri",string.Format("Unable to resolve binding for Uri: {0}",uri));
            }
            return binding;
        }
    }
}
